package com.earth.amazeadmin.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Created by dongjigong on 2018/5/8.
 *
 * @Email osbornjonny@126.com
 * @Description
 */
@Controller
public class LoginController {

    private Logger log = LoggerFactory.getLogger(getClass());

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginForm(Model model) {
        return "login";
    }


    @RequestMapping("/logon")
    public String loginUser(String username, String password, boolean rememberMe, RedirectAttributes redirectAttributes) {


        Subject currentUser = SecurityUtils.getSubject();
        // let's login the current user so we can check against roles and permissions:
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        if (!currentUser.isAuthenticated()) {
            if (rememberMe) {
                token.setRememberMe(true);
            }
            try {
                currentUser.login(token);
            } catch (UnknownAccountException uae) {
                log.info("There is no user with username of " + token.getPrincipal());
                redirectAttributes.addFlashAttribute("message", "未知账户");
            } catch (IncorrectCredentialsException ice) {
                log.info("Password for account " + token.getPrincipal() + " was incorrect!");
                redirectAttributes.addFlashAttribute("message", "密码不正确");
            } catch (LockedAccountException lae) {
                log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                        "Please contact your administrator to unlock it.");
                redirectAttributes.addFlashAttribute("message", "账户已锁定");
            }
            // ... catch more exceptions here (maybe custom ones specific to your application?
            catch (AuthenticationException ae) {
                //unexpected condition?  error?
            }
        } else {
            token.clear();
            return "redirect:/login";
        }

        //say who they are:
        //print their identifying principal (in this case, a username):
        log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
        return "redirect:/index";

    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(RedirectAttributes redirectAttributes) {
        //使用权限管理工具进行用户的退出，跳出登录，给出提示信息
        try {
            SecurityUtils.getSubject().logout();
            redirectAttributes.addFlashAttribute("message", "您已安全退出");
        } catch (UnknownSessionException e) {
            log.info("User [" + e + "] logged out successfully.");
        }
        return "redirect:/login";
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register() {
        return "sign-up";
    }

}
